package com.flowersinc.shop.views
{
	import com.flowersinc.shop.events.HomeViewEvent;
	import com.flowersinc.shop.model.Flower;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.Image;
	
	import org.goverla.managers.ApplicationManager;
	
	[Event(name="flowerSelected", type="com.flowersinc.shop.events.HomeViewEvent")]

	public class HomeViewBase extends Canvas
	{		
		
		public var image1 : Image;
		public var image2 : Image;
		public var image3 : Image;
		public var image4 : Image;
		public var image5 : Image;
		public var image6 : Image;
		public var image7 : Image;
		public var image8 : Image; 
		public var image9 : Image;
		
		[Bindable]
		protected var imageSource : String;
		
		[Bindable]
		protected var patternImageUrl : String;
		
		private var images : Array;
		
		[Bindable]	
		public function get flowers() : ArrayCollection
		{
			return _flowers;
		}
		
		public function set flowers(value : ArrayCollection) : void 
		{
			_flowers = value;
			
			if (value != null) 
			{
				initializeImages();	
			}
		} 
		
		public function startShuffle() : void 
		{
			_timer.start();
		}
		
		public function stopShuffle() : void 
		{
			_timer.stop();
		}
		
		protected function onInitialize() : void 
		{
			patternImageUrl = ApplicationManager.instance.url + "images/lines-pattern.png";
		}
		
		protected function onCreationComplete() : void 
		{
			images = [image1, image2, image3, image4, image5, image6, image7, image8, image9];
			
			if (flowers != null) 
			{
				initializeImages();
			}
			startTimer();
		}
		
		protected function onImageClick(event : MouseEvent) : void 
		{
			var	homeEvent : HomeViewEvent = new HomeViewEvent(HomeViewEvent.FLOWER_SELECTED);
			
			homeEvent.selectedFlower = Flower(Image(event.currentTarget).data); 
			dispatchEvent(homeEvent);
		}	
		
		private function initializeImages() : void 
		{
			if (!_imagesInitialized) 
			{
				for (var i : int = 0; i < images.length; i++) 
				{	
					images[i].data = flowers[i];
					images[i].source = flowers[i].thumbImage;
				}
				
				_imagesInitialized = true;
			}
		}
		
		private function startTimer() : void 
		{
			_timer = new Timer(1500);

			_timer.addEventListener(TimerEvent.TIMER, onTick);		
			_timer.start();
		}
		
		private function onTick(event : TimerEvent) : void 
		{
			images[_currentImageIndex].data = flowers[_currentFlowerIndex];
			images[_currentImageIndex].source = flowers[_currentFlowerIndex].thumbImage;
			_currentImageIndex += 1;
			_currentFlowerIndex += 1;
			
			if (_currentFlowerIndex >= flowers.length)  
			{
				_currentFlowerIndex = 0;
				
			}
			
			if (_currentImageIndex >= images.length) 
			{
				_currentImageIndex = 0;
			}
		}
		
		private var _imagesInitialized : Boolean;
		private var _timer : Timer;
		private var _currentImageIndex : int;
		private var _currentFlowerIndex : int = 9;
		private var _flowers : ArrayCollection;
	}
}